package yxy.game.pm2.command.pickedtroops;

import yxy.apple.util.RandomUtility;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.commandertally;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PickedTroopsSlot;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedTroopsMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.pickedtroops.PickedTroopsHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/****************************************
 * @name CMD_PickedTroopsSlotsCompound
 * @description 兵符合成
 * @author 梁健彬
 * @since 2022-07-25
 ***************************************/
@MessageType(proto = Command.PickedTroopsSlotsCompound, classtype = PickedTroopsMessage.PickedTroopsSlotsCompoundRequest.class)
public class CMD_PickedTroopsSlotsCompound extends Command<PickedTroopsMessage.PickedTroopsSlotsCompoundRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotsCompoundRequest request) {
        String playerCode = messageSource.getTag();
        List<String> codes = request.getCodesList();//兵符code集合
        PickedTroopsMessage.PickedTroopsSlotsCompoundResponse.Builder response = PickedTroopsMessage.PickedTroopsSlotsCompoundResponse.newBuilder();

        try {
            //todo 检查所有的兵符及消耗和条件
            commandertally data = null;
            ArrayList<PickedTroopsSlot> slots = new ArrayList<>();
            for (String code : codes) {
                PickedTroopsSlot slot = PickedTroopsSlot.get(playerCode, code);
                if (data == null) {
                    data = DataCenter.getData(slot.getDataId(), commandertally.class);
                }
                if (slot == null || slot.getDataId() != data.getId() || slot.getType() != data.getType()) {//检查是否同一品级,同一阵营
                    send(messageSource, response, Result.PickedTroopsSlotNeedSameSlot);//兵符合成需要同一阵营或同一等级
                    return;
                }
                if (slot.getUse()) {
                    send(messageSource, response, Result.PickedTroopsSlotIsUsing);//兵符正在使用
                }
                slots.add(slot);
            }

            //下一级的兵符
            commandertally nextdata = DataCenter.getData(data.getUpgradeid(), commandertally.class);
            if (nextdata == null || data.getUpheave() != 1) {
                send(messageSource, response, Result.ParamError);//下一级不存在或不能再继续合成返回信息
                return;
            }

            Currency cost = Currency.create(data.getUpcosttype(), data.getUpcostid(), data.getUpcostnum());//消耗
            try {
                new CurrencyHandle().cost(playerCode, CCReason.兵符, cost);
            } catch (CurrencyNotEnoughException e) {
                send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                return;
            }

            int rate = 0;
            switch (slots.size()) {
                case 2:
                    rate = data.getRate2();
                    break;
                case 3:
                    rate = data.getRate3();
                    break;
                case 4:
                    rate = data.getRate4();
                    break;
                case 5:
                    rate = data.getRate5();
                    break;
                default:
                    send(messageSource, response, Result.ParamError);//一个或多个不能合成
                    return;
            }

            //todo 合成失败，发补偿，成功发兵符
            if (RandomUtility.ppm() > rate) {
                // 合成失败 发补偿
                ArrayList<Currency> curs = new ArrayList<>();
                for (int i = 0; i < slots.size() / 2; i++) {
                    Collection<Currency> collection = Currency.create(data.getReturndestype(), data.getReturndesid(), data.getReturndesnum());
                    curs.addAll(collection);
                }
                new CurrencyHandle().addCurrency(playerCode, CCReason.兵符, curs);
                response.addAllItem(new CurrencyHandle().packItems(curs));
                response.setSuccess(false);
            } else {
                // 合成成功
                PickedTroopsSlot slot = PickedTroopsSlot.create(nextdata, playerCode);
                response.setSlots(PickedTroopsHandle.getSlot(slot));
                response.setSuccess(true);
            }

            response.addAllCodes(codes);
            PickedTroopsSlot.remove(playerCode, slots);


            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }

    }

    private void send(MessageSource messageSource, PickedTroopsMessage.PickedTroopsSlotsCompoundResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}